#! -*- coding:utf-8 -*-
import fcntl
import json
import logging
import os
import sys

from django.core.management.base import BaseCommand

from common.transaction.model import Withdraw, WITHDRAW_STATUS
from common.transaction.db import withdraw_back
from django.conf import settings

_LOGGER = logging.getLogger('worker')


class Command(BaseCommand):

    def handle(self, **kwargs):
        items = Withdraw.query.filter(Withdraw.status==WITHDRAW_STATUS.FAIL).all()
        for item in items:
            withdraw_id = item.id
            print 'ready to back withdraw {}'.format(withdraw_id)
            withdraw_back(0, withdraw_id, u'系统自动返款')
            print 'withdraw {} back done'.format(withdraw_id)
